package com.atguigu.eduservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.mapper.EduTeacherMapper;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-10-15
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    //查询前4条名师
    @Override
    @Cacheable(value = "teacher",key = "'selectTeacherList'")
    public List<EduTeacher> SelectAllTeacher() {
        //查询前4条名师
        QueryWrapper<EduTeacher> wrapperTeacher = new QueryWrapper<>();
        wrapperTeacher.orderByDesc("id");
        wrapperTeacher.last("limit 4");
        List<EduTeacher> list = baseMapper.selectList(wrapperTeacher);
        return list;
    }

    //1 分页查询讲师的方法
    @Override
    public Map<String, Object> getTeacherFrontList(Page<EduTeacher> pageTeacher) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        //把分页数据封装到pageTeacher对象
        baseMapper.selectPage(pageTeacher,wrapper);

        List<EduTeacher> records = pageTeacher.getRecords();//每页数据list集合
        long current = pageTeacher.getCurrent();//当前页
        long pages = pageTeacher.getPages();//总页数
        long size = pageTeacher.getSize();//每页记录数
        long total = pageTeacher.getTotal();//总记录数
        boolean hasNext = pageTeacher.hasNext();//当前是否有下一页
        boolean hasPrevious = pageTeacher.hasPrevious();//当前是否有上一页

        //把分页数据获取出来，放到map集合
        Map<String,Object> map = new HashMap<>();
        map.put("items",records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;
    }

    @Override
    public R getTeacherList(Page<EduTeacher> pageTeacher, TeacherQuery teacherQuery) {
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询
        //mybaits学过 动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(name)){
            //构建条件
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);//eq等于
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);//ge大于等于,gmt_create是数据库表中字段名称
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);//le小于等于
        }
        wrapper.orderByDesc("gmt_create");
        //把分页数据封装到pageTeacher对象
        baseMapper.selectPage(pageTeacher,wrapper);
        List<EduTeacher> records = pageTeacher.getRecords();//每页数据list集合
        long total = pageTeacher.getTotal();//总记录数
        //map返回
        return R.ok().data("total",total).data("rows",records);
    }

    @Override
    public boolean addTeacher(EduTeacher eduTeacher) {
        int i = baseMapper.insert(eduTeacher);
        if (i>0)
            return true;
        else
            return false;
    }
}

